package com.example.mobile2021606128wje0512;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Switch;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;

import com.example.mobile2021606128wje0512.fragment.IndexFragment;
import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private FrameLayout framelayout;
    private NavigationView navigation;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        initView();
        setSupportActionBar( toolbar );
        getSupportActionBar().setDisplayHomeAsUpEnabled( true );
        getSupportActionBar().setHomeButtonEnabled( true );
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawerLayout, toolbar, R.string.app_name, R.string.app_name ) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened( drawerView );
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed( drawerView );
            }
        };
        toggle.syncState();//设置为同步
        drawerLayout.addDrawerListener( toggle );
        //初始化Fragment
        getSupportFragmentManager().beginTransaction()
                .add( R.id.framelayout, IndexFragment.newInstance( "", "" ), "" )
                .commit();
        toolbar.setTitle( "智能交通系统" );
        //给测滑菜单设置选择事件
        navigation.setNavigationItemSelectedListener( new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull @org.jetbrains.annotations.NotNull MenuItem item) {
                this.item = item;
                item.setCheckable( true );
                Switch( item.getItemId() ) {
                    case R.id.itemIndex:
                        getSupportFragmentManager().beginTransaction()
                                .replace( R.id.framelayout, IndexFragment.newInstance( "", "" ), "" )
                                .commit();
                        toolbar.setTitle( "智能交通系统" );
                        break;
                    case R.id.itemETC:
                        getSupportFragmentManager().beginTransaction()
                                .replace( R.id.framelayout, IndexFragment.newInstance( "", "" ), "" )
                                .commit();
                        toolbar.setTitle( "ETC账户" );
                        break;
                    case R.id.itemLight:
                        getSupportFragmentManager().beginTransaction()
                                .replace( R.id.framelayout, IndexFragment.newInstance( "", "" ), "" )
                                .commit();
                        toolbar.setTitle( "交通灯" );
                        break;
                    case R.id.itemEnvi:
                        getSupportFragmentManager().beginTransaction()
                                .replace( R.id.framelayout, IndexFragment.newInstance( "", "" ), "" )
                                .commit();
                        toolbar.setTitle( "环境指标" );
                        break;
                    case R.id.itemExit:
                        getSupportFragmentManager().beginTransaction()
                                .replace( R.id.framelayout, IndexFragment.newInstance( "", "" ), "" )
                                .commit();
                        toolbar.setTitle( "退出" );
                        break;

                }
                drawerLayout.closeDrawer(drawerLayout);
                return true;
            }
        } );


  }

        private void initView () {
            toolbar = (Toolbar) findViewById( R.id.toolbar );
            drawerLayout = (DrawerLayout) findViewById( R.id.drawerLayout );
            framelayout = (FrameLayout) findViewById( R.id.framelayout );
            navigation = (NavigationView) findViewById( R.id.navigation );
            }

    }
